package router

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

type  WhiteList struct {
	url []string
}
var data WhiteList


var FilterUser = func(ctx *context.Context) {
	open := false
	u := ctx.Request.RequestURI
	defer func() {
		if !open {
			ctx.Redirect(302, "/login")
		}
	}()

	for _,v := range data.url{
		if u == v{
			open = true
		}
	}
	if ctx.Input.CruSession.Get("sessionState") != nil{
		open = true
		if u == "/" || u == "/login"{
			ctx.Redirect(302, "/index")
		}
	}
}

func setWhiteList()  {
	data = WhiteList{
		url:[]string{
			"/login",
		},
	}
}

func init() {
	setWhiteList()
	beego.InsertFilter("/*",beego.BeforeRouter,FilterUser)
}